package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.Contract;
import com.asm.entity.sys.ContractContain;
import com.asm.repository.ContractContainRepository;
import com.asm.repository.ContractRepository;
import com.asm.vo.sys.SpotScheduleVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhuangGuiMan
 */
@Service
public class SpotScheduleService {

    @Resource
    private ContractContainRepository contractContainRepository;

    @Resource
    private ContractRepository contractRepository;

    /**
     * 根据广告位编号分页查询广告位档期
     *
     * @param id         广告位编号
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 广告位档期
     */
    public PageResult<SpotScheduleVo> getSpotScheduleBySpotId(Long id, Integer pageNumber, Integer pageSize) {
        Page<ContractContain> contractContains = contractContainRepository.findBySpotId(id, PageRequest.of(pageNumber, pageSize));
        List<SpotScheduleVo> list = new ArrayList<>();
        for (ContractContain contractContain : contractContains) {
            Contract contract = contractRepository.findContractById(contractContain.getContractId());
            SpotScheduleVo spotScheduleVo = new SpotScheduleVo();
            spotScheduleVo.setStartDate(contract.getContractDate());
            spotScheduleVo.setEndDate(contract.getContractEndDate());
            list.add(spotScheduleVo);
        }
        return new PageResult<>(pageNumber, pageSize, contractContains.getTotalPages(), list);
    }
}
